package com.zlj.response;

import java.io.FileInputStream;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;

/**
 * response的两个流：
 * 
 * ServletOutputStream：用来向客户端发送字节数据   ServletOutputStream out = response.getOutputStream();
 * PrintWriter:用来向客户端发送字符数据，需要设置编码  PrintWriter pw = response.getWriter();
 * 
 * 两个流不能同时使用
 */
@WebServlet("/FServlet")
public class FServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		/*
			String str = "Hello Stream";
			byte[] bytes = str.getBytes();   //把字符转换为字节数组
			response.getOutputStream().write(bytes);  //输出到页面上
        */
		
		/*
		 * 演示响应字节数据
		 * 
		 * 把一张图片读取到字节数组中
		 */
		String path = "C:/Users/bashen/Desktop/01.png";
		FileInputStream in = new FileInputStream(path);
		byte[] bytes = IOUtils.toByteArray(in);   //读取输入流中的内容字节到字节数组中
		response.getOutputStream().write(bytes);
	}
}
